package com.monitor.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.ErpProject;
import com.monitor.entity.ErpProjectList;
import com.monitor.service.ErpProjectListService;
import com.monitor.service.ErpProjectService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务收费表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-15
 */
@RestController 
@RequestMapping("/erpProject")
@Api(description="服务收费")
public class ErpProjectController {

    @Resource
    private ErpProjectService projectService;
    @Resource
    private ErpProjectListService projectListService;

    @RequestMapping("/inertOrUpdateProject")
    @ApiOperation("添加Or修改服务收费")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateProject(String json) {   //少了客户id没传
        //ErpProject project, List<ErpProjectList> projectLists
        Result<Integer> result=new Result<Integer>();
        try{
            ErpProject project = JSON.parseObject(json, ErpProject.class);
            List<ErpProjectList> projectLists = project.getProjectLists();
            projectService.insertOrUpdate(project);
            for (ErpProjectList projectList:projectLists) {
                projectList.setProjectId(project.getId());
                projectListService.insertOrUpdate(projectList);
            }
            result.setCount(1);
            result.setData(null);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getProject")
    @ApiOperation("查询服务收费")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<List<ErpProject>> getProject() {
        Result<List<ErpProject>> result=new Result<List<ErpProject>>();
        try{
            List<ErpProject> projects=projectService.selectList(new EntityWrapper<ErpProject>());
            for (ErpProject project:projects) {
                List<ErpProjectList> projectLists=projectListService.selectList(new EntityWrapper<ErpProjectList>().eq("project_id",project.getId()));
                project.setProjectLists(projectLists);
            }
            result.setCount(1);
            result.setData(projects);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

@RequestMapping("/getProjectById")
    @ApiOperation("根据id查询服务收费")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpProject> getProjectById(Integer projectId) {
        Result<ErpProject> result=new Result<ErpProject>();
        try{
            ErpProject projects=projectService.selectOne(new EntityWrapper<ErpProject>().eq("id",projectId));
            List<ErpProjectList> projectLists=projectListService.selectList(new EntityWrapper<ErpProjectList>().eq("project_id",projectId));
            projects.setProjectLists(projectLists);
            result.setCount(1);
            result.setData(projects);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeProjectById")
    @ApiOperation("根据id删除服务收费")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> removeProjectById(Integer projectId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(projectListService.deleteById(projectId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

}

